#include "Turn.h"
#include <stdio.h>
#include <stdlib.h>

void showTurn(int *pTurn) {
	int realTurn;
	realTurn = *pTurn / 2 + *pTurn % 2;
	printf(" TURN %d (%s)\n\n", realTurn, *pTurn % 2 ? "Black" : "White");
}

int getTurn(int *pTurn) {
	return *pTurn;
}

void Turn(Board *pBoard, int *pTurn, Coordinates *pCoordinates) {// trzeba pozmieniac if'y i elsy
	int forcibleMoveCheck;
	int movePosibility;
	system("cls");
	showTurn(pTurn);
	countDraughts(pBoard);
	drawBoard(pBoard);
	forcibleMoveCheck = checkForcibleMove(pBoard, pTurn);

	if(forcibleMoveCheck == FALSE) { // czy cos == FALSE to to samo co !cos ??
		printf("forcibleMove FALSE - beda podawane coordinates");
		getchar();
		getCoordinates(pBoard, pCoordinates, pTurn);
		printf("wybrane y i x i dir to: %d      %d    %c \t bo chodzi o 0 1 2 lub 3\n\n",pCoordinates->row, pCoordinates->column, pCoordinates->dir);
		getchar();
		movePosibility = checkMovementPosibility(pBoard, pTurn, pCoordinates);
		printf("wartosc movePosibility = %d", movePosibility);
		getchar();
		if(movePosibility) {
			moveChecker(pBoard, pTurn, pCoordinates);
		}
		else
			Turn(pBoard, pTurn, pCoordinates);
	}
	else
		printf("%d forcibleMoveCheck powinno byc TRUE (1)",forcibleMoveCheck);
}

boolean isEndGameCondition(Board *pBoard) {
	Field field;
	int i;
	int j;
	int blackCounter = 0;
	int whiteCounter = 0;
	for(i = 0; i < 8; i++) {
		for(j = 0; j < 4; j++) {
			field = getField(pBoard, j, i);
			if (field.state == STATE_BLACK)
				blackCounter++;
			else if (field.state == STATE_WHITE)
				whiteCounter++;
		}
	}
	if(blackCounter == 0) {
		printf("GAME OVER. WHITE WINS.");
		getchar();
	}
	else if(whiteCounter == 0) {
		printf("GAME OVER. BLACK WINS.");
		getchar();
	}

	return (blackCounter == 0 || whiteCounter == 0); 
}

void countDraughts(Board *pBoard) {
	Field field;
	int i;
	int j;
	int blackCounter = 0;
	int whiteCounter = 0;
	for(i = 0; i < 8; i++) {
		for(j = 0; j < 4; j++) {
			field = getField(pBoard, j, i);
			if (field.state == STATE_BLACK)
				blackCounter++;
			else if (field.state == STATE_WHITE)
				whiteCounter++;
		}
	}
	printf("White draugths : %d  ###  Black draughts : %d\t\n\n", whiteCounter, blackCounter);
}
